package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jdbc.ConnectionJDBC;
import com.util.ReadWirtePropertis;

public class GetConnectServlet extends HttpServlet {

	/**
     * serialVersionUID
     */
    private static final long serialVersionUID = 1L;

    /**
	 * Constructor of the object.
	 */
	public GetConnectServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Connection con = null ;
		boolean flag = true;
		request.setAttribute("connect", "");
	    try 
	    {
	        String dbname = request.getParameter("dbname");
	        String driver = request.getParameter("driver");
	        String ip  = request.getParameter("ip");
	        String user  = request.getParameter("user");
	        String port = request.getParameter("port");
	        String pwd = request.getParameter("pwd");
	        String url = "";
	        if (driver.contains("mysql"))
            {
	            url =  "jdbc:mysql://" + ip + ":" + port + "/" + dbname;
            }
	        else if(driver.contains("sqlserver"))
	        {
	            url =  "jdbc:sqlserver://" + ip + ":" + port + ";DatabaseName=" + dbname;
            }
	        else if(driver.contains("oracle"))
	        {
	            url =  "jdbc:oracle:thin:@" + ip + ":" + port + ":" + dbname;
            }
	        else if(driver.contains("postgresql"))
	        {
//	        	jdbc:postgresql://127.0.0.1:5432/security_db
	            url =  "jdbc:postgresql://" + ip + ":" + port + "/" + dbname;
            }
	    	con = ConnectionJDBC.testConnection(driver,url ,user,pwd);
	    	if(con == null)
	    	{
	    		 flag = false;
	    		 request.setAttribute("connect", "connect fail");
	    		 request.getRequestDispatcher("/index.jsp").forward(request,response); 
	    	}
	    	else
	    	{
//	    		ConnectionJDBC.DBDRIVER = driver;
//	    		ConnectionJDBC.DBURL = url;
//	    		ConnectionJDBC.DBUSER = user;
//	    		ConnectionJDBC.DBPASS = pwd;
    	        ReadWirtePropertis.updateProperties("datasource.driverClassName",driver);
    	        ReadWirtePropertis.updateProperties("datasource.url",url);
    	        ReadWirtePropertis.updateProperties("datasource.username",user);
    	        ReadWirtePropertis.updateProperties("datasource.password",pwd);
    	        request.setAttribute("schema", dbname);
            }
		} catch (Exception e) 
	    {
			 flag = false;
			 request.setAttribute("connect", "connect fail");
			 request.getRequestDispatcher("/index.jsp").forward(request,response); 
		}
	    finally
	    {
	    	if(con != null)
	    	{
	    		try 
	    		{
					con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	    	}
	    }
	    if (flag) 
	    {
	    	request.getRequestDispatcher("/gettable").forward(request,response); 
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
